//
//  ST_BTSheetActionController.swift
//  btvideo
//
//  Created by mac on 2020/3/3.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit

//通用的 Sheet Action
class ST_BTSheetActionController: UIViewController {
    
//    MARK: - Init && life cycle
    public convenience init(title: String?, message: String?){
        self.init(nibName: nil, bundle: nil)
        self.sheet_title = title
        self.message = message
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
//        暂时未实现 title message
        
//        添加取消btn
        view.addSubview(cancel_btn)
        cancel_btn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(sheet_action_height)
            make.bottom.equalToSuperview().offset(-19-BTSafeAreaBottom)
        }
        
        view.addSubview(sheet_root_view)
        sheet_root_view.snp.makeConstraints { (make) in
            make.left.right.equalTo(cancel_btn)
            make.height.equalTo(actions.count.cgFloat*sheet_action_height+actions.count.cgFloat-1)
            make.bottom.equalTo(cancel_btn.snp.top).offset(-10)
        }
        
        view.addSubview(close_btn)
        close_btn.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(sheet_root_view.snp.top).offset(-20)
        }
        
        var belowBtn:UIButton?
        
        for (idx,item) in actions.reversed().enumerated(){
            
            let btn = UIButton.init(type: UIButton.ButtonType.custom)
            
            btn.backgroundColor = sheet_action_background_color
            btn.tag = baseTag + idx
            btn_states.forEach { btn.setAttributedTitle(item.title, for: $0) }
            sheet_root_view.addSubview(btn)
            btns.append(btn)
            
            btn.snp.makeConstraints { (make) in
                make.height.equalTo(sheet_action_height)
                make.left.right.equalToSuperview()
                if let below = belowBtn{
                    make.bottom.equalTo(below.snp.top).offset(-1)
                }else{
                    make.bottom.equalToSuperview()
                }
            }
            btn.addTarget(self, action: #selector(click(sender:)), for: UIControl.Event.touchUpInside)
            
            belowBtn = btn
            
            if idx != actions.count - 1 && actions.count != 1{

                let line = UIView()
                line.backgroundColor = UIColor.init(hex: 0xF5F7FA)
                view.addSubview(line)
                line.snp.makeConstraints { (make) in
                    make.height.equalTo(1)
                    make.left.right.equalTo(btn)
                    make.bottom.equalTo(btn.snp.top)
                }
            }
            
            
        }
        
        
        
        
        
    }
//    MARK: - setting Properties
    var sheet_action_background_color = UIColor.white
    var sheet_action_height = 44.cgFloat
//    MARK: - Properties
    fileprivate var sheet_title: String?
    fileprivate var message: String?
    fileprivate var baseTag = 1037

    fileprivate var actions = [ST_BTAlertAction]()
    
    fileprivate var btns = [UIButton]()
    
    fileprivate var btn_states: [UIControl.State] {
        return [.normal, .selected, .highlighted, .disabled]
    }
    
    fileprivate lazy var close_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.addTarget(self, action: #selector(cancel_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var cancel_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        let cancelString = NSMutableAttributedString(string: "取消")
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)]
        
        cancelString.addAttributes(attr, range: NSRange(location: 0, length: cancelString.length))
        
        btn_states.forEach { control.setAttributedTitle(cancelString, for: $0) }
        
        control.tag = self.baseTag
        
        control.cornerRadius = 6
        
        control.backgroundColor = sheet_action_background_color
        
        control.addTarget(self, action: #selector(cancel_handle(sender:)), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    fileprivate var sheet_root_view:UIView = {
       let control = UIView()
        control.backgroundColor = .white
        control.cornerRadius = 6
        return control
    }()
}

//    MARK: - Event
extension ST_BTSheetActionController{
    
    @objc fileprivate func click(sender:UIButton){
        
        self.goback_previous_controller(animated: true) {
         
            guard let action = self.actions.reversed()[safe:sender.tag - self.baseTag] else { return }
            
            action.handler?(action)
        }
    }
    
    @objc fileprivate func cancel_handle(sender:UIButton){
        
        self.goback_previous_controller()
    }
    
}

//    MARK: - Public Method
extension ST_BTSheetActionController{
    
    func addAction(_ action: ST_BTAlertAction){
        actions.append(action)
    }
    
    func show(){
        
        self.overCurrentContext = true
        
        GlobalPresent(toController: self)
        
    }
}

//    MARK: - ST_BTAlertAction
class ST_BTAlertAction: NSObject {
    
    public convenience init(title: NSAttributedString?, handler: ((ST_BTAlertAction) -> Void)? = nil){
        self.init()
        self.title = title
        self.handler = handler
    }
    
    override init() {
        super.init()
    }
     
    fileprivate var title:NSAttributedString?
    
    fileprivate var handler:((ST_BTAlertAction) -> Void)?
    
}
